var serverData = {};
var serverTable;
var serverScadaBasePointTable;
var serverScadaBasePointData = {};
var ScadaRtuBaseInfoId;
jQuery(document).ready(function () {
	serverTable = jQuery('#dataTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/scadartubase/getpage',
            "data": function (d) {
                return $.extend({}, d, serverData);
            }
        },
        columns: [
            {
            	data: null
            },
            {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildContract">';
	            	return operate;
	            }   
            },
            {
                data: 'name'
            },
            {
            	data: 'alias'
            },
            {
            	data: 'user.name'
            },
            {
                data: 'createTime',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD');
                    else
                        return '';
                }
            },
            {
                data: 'memo'
            }
            ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }
            serverTable.off('click', 'tr');
            serverTable.on('click','tr',function(event) {
            	event = event|| window.event;
            	var target = event.target|| event.scrElement;
            	var data = serverTable.row(this).data();
            	serverScadaBasePointData.ScadaRtuBaseInfoId = data.id;
            	serverScadaBasePointTable.draw();
            	jQuery('#ScadaRtuBaseInfoId').val(data.id);
            	ScadaRtuBaseInfoId = data.id;
            	return;
            });
        }
    });
	serverScadaBasePointTable = jQuery('#ScadaBasePointTable').DataTable({
	    "serverSide": true,
	    "ordering": false,
	    "iDisplayLength": 5,
	    "ajax": {
	        "dataType": "json",
	        "type": "get",
	        "cache": false,
	        "url":window.rootPath+ '/ui/scadartubase/getpageScadaBasePoint',
	        "data": function (d) {
	            return $.extend({}, d, serverScadaBasePointData);
	        }
	    },
	    columns: [
	        {
	        	data: null
	        },
	        {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildCruxDate">';
	            	return operate;
	            }   
	        },
	        {
	        	data: 'name'
	        },
	        {
	            data: 'alias'
	        },
	        {
	        	data: 'pointType',//0实点，1虚点
	        	render: function (data, type, full, meta) {
	              	var dOut;
	              	 if(data=='0'){
	              		dOut = '<td class="center" >实点</td>';
	              	}
	              	else if(data=='1'){
	              		dOut = '<td class="center" >虚点</td>';
	              	}
	            	return dOut;
	              }
	        },
	        {
	            data: 'dataType',//0 模拟量  1 开关量
	            render: function (data, type, full, meta) {
	              	var dOut;
	              	 if(data=='0'){
	              		dOut = '<td class="center" >模拟量</td>';
	              	}
	              	else if(data=='1'){
	              		dOut = '<td class="center" >开关量</td>';
	              	}
	            	return dOut;
	              }
	        },
	        {
	        	data: 'unit'
	        },
	        {
	        	data: 'upperLimit'
	        },
	        {
	        	data: 'lowLimit'
	        },
	        {
	        	data: 'dataField'
	        },
	        {
	        	data: 'user.name'
	        },
	        {
	        	data: 'createTime',
	            render: function (data, type, full, meta) {
	                if (data)
	                    return moment(data).format('YYYY/MM/DD');
	                else
	                    return '';
	            }
	        },
	        {
	        	data: 'memo'
	        }
	        ],
	        "drawCallback": function (oSettings) {
	            var k = 0;
	            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	
	            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	            }
	         }
    });
	
	jQuery('.addscadaBtn').on('click', function () {
		jQuery('#ScadaRtuid').val('');
		jQuery('#ScadaRtuname').val('');
		jQuery('#ScadaRtualias').val('');
		jQuery('#ScadaRtumemo').val('');
		jQuery('#ScadaRtucreateTime').val('');
		jQuery('#divideScadaModal').modal('show');
	});
	jQuery('.eidtscadaBtn').on('click', function () {
		var selects = getSelect("#dataTable", 1);
		if(selects.length==0){
			$.springLemonDialog({
		         content: '请选择记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		     }); 
		}else if(selects.length>1){
			$.springLemonDialog({
				content: '请选择记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			}); 
		}else if(selects.length==1){
			var ScadaRtuid = selects[0].id;
			var ScadaRtuname = selects[0].name;
			var ScadaRtualias = selects[0].alias;
			var ScadaRtumemo = selects[0].memo;
			var ScadaRtucreateTime = selects[0].createTime;
			jQuery('#ScadaRtuid').val(ScadaRtuid);
			jQuery('#ScadaRtuname').val(ScadaRtuname);
			jQuery('#ScadaRtualias').val(ScadaRtualias);
			jQuery('#ScadaRtumemo').val(ScadaRtumemo);
			jQuery('#ScadaRtucreateTime').val(ScadaRtucreateTime);
			jQuery('#divideScadaModal').modal('show');
		}
	});
	jQuery('.saveScadaBtn').on('click', function () {
		var id = jQuery('#ScadaRtuid').val();
		var name = jQuery('#ScadaRtuname').val();
		var alias = jQuery('#ScadaRtualias').val();
		var memo = jQuery('#ScadaRtumemo').val();
		var createTime = jQuery('#ScadaRtucreateTime').val();
		var d;
		if(id == ""){
			d = {
				"name" : name,
				"alias" : alias,
				"memo" : memo,
				"createTime" : new Date(),
			};
		}else{
			d = {
				"id" : id,	
				"name" : name,	
				"alias" : alias,	
				"memo" : memo,	
				"createTime" : createTime,
			};
		}
		var url=window.rootPath+'/ui/scadartubase/saveScadaRtuBaseInfo';
        jQuery.ajax({
            url:encodeURI(url),
            type:'post',
            data:JSON.stringify(d),
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success: function () {
                jQuery('#divideScadaModal').modal('hide');
                jQuery('#ScadaRtuid').val('');
        		jQuery('#ScadaRtuname').val('');
        		jQuery('#ScadaRtualias').val('');
        		jQuery('#ScadaRtumemo').val('');
        		jQuery('#ScadaRtucreateTime').val('');
        		serverTable.draw();
            }
        });
	});
	//上面table
	jQuery('.searchBtn').on('click', function () {
		serverData.key = jQuery('#keyword').val();
		serverTable.draw();
	});
	jQuery('.resetBtn').on('click', function () {
		jQuery('#keyword').val("");
		serverData.key = "";
		serverTable.draw();
	});
	//下面table
	jQuery('.searchBtn1').on('click', function () {
		serverScadaBasePointData.key = jQuery('#keyword1').val();
		serverScadaBasePointData.ScadaRtuBaseInfoId = ScadaRtuBaseInfoId;
		serverScadaBasePointTable.draw();
	});
	jQuery('.resetBtn1').on('click', function () {
		jQuery('#keyword1').val("");
		serverScadaBasePointData.key = "";
		serverScadaBasePointData.ScadaRtuBaseInfoId = ScadaRtuBaseInfoId;
		serverScadaBasePointTable.draw();
	});
	
	//虚点
	jQuery('#VirtualPointBtn').on('click', function () {
		if(ScadaRtuBaseInfoId != undefined && ScadaRtuBaseInfoId != ""){
			jQuery('#pointType').val('1');
			jQuery('#ScadaBasePointname').val('');
			jQuery('#ScadaBasePointalias').val('');
			jQuery('#UpperLimit').val('');
			jQuery('#lowLimit').val('');
			jQuery('#SwitchingValue').val('');
			jQuery('#DataType').val("1");
			jQuery('#ScadaBasePoint').val('');
			jQuery('#ScadaBasePointModal').modal('show');
		}else{
			alert("请选择单元！");
		}
	});
	//实点
	jQuery('#realPointBtn').on('click', function () {
		if(ScadaRtuBaseInfoId != undefined && ScadaRtuBaseInfoId != ""){
			jQuery('#pointType').val('0');
			jQuery('#ScadaBasePointname').val('');
			jQuery('#ScadaBasePointalias').val('');
			jQuery('#UpperLimit').val('');
			jQuery('#lowLimit').val('');
			jQuery('#SwitchingValue').val('');
			jQuery('#DataType').val("1");
			jQuery('#ScadaBasePoint').val('');
			jQuery('#ScadaBasePointModal').modal('show');
		}else{
			alert("请选择单元！");
		}
	});
	jQuery('#editScadaBasePointBtn').on('click', function () {
		var selects = getSelect("#ScadaBasePointTable", 1);
		if(selects.length==0){
			$.springLemonDialog({
		         content: '请选择记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		     }); 
		}else if(selects.length>1){
			$.springLemonDialog({
				content: '请选择记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			}); 
		}else if(selects.length==1){
			var ScadaBasePointId = selects[0].id;
			var pointType = selects[0].pointType;
			var ScadaBasePointname = selects[0].name;
			var ScadaBasePointalias = selects[0].alias;
			var dataField = selects[0].dataField;
			var DataType = selects[0].dataType;
			var SwitchingValue = selects[0].switchingValue;
			var UpperLimit = selects[0].upperLimit;
			var lowLimit = selects[0].lowLimit;
			var unit = selects[0].unit;
			var ScadaBasePointmemo = selects[0].memo;
			var ScadaBasePointcreateTime = selects[0].createTime;
			jQuery('#ScadaBasePointId').val(ScadaBasePointId);
			jQuery('#pointType').val(pointType);
			jQuery('#ScadaBasePointname').val(ScadaBasePointname);
			jQuery('#ScadaBasePointalias').val(ScadaBasePointalias);
			jQuery('#dataField').val(dataField);
			jQuery('#DataType').val(DataType);
			if(DataType=="1"){
				jQuery('#SwitchingValue').val(SwitchingValue);
				jQuery('#UpperLimit').val('');
				jQuery('#lowLimit').val('');
				document.getElementById("UpperLimit").disabled = true;
				document.getElementById("lowLimit").disabled = true;
				document.getElementById("SwitchingValue").disabled = false;
			}
			if(DataType=="0"){
				jQuery('#SwitchingValue').val('');
				jQuery('#UpperLimit').val(UpperLimit);
				jQuery('#lowLimit').val(lowLimit);
				document.getElementById("UpperLimit").disabled = false;
				document.getElementById("lowLimit").disabled = false;
				document.getElementById("SwitchingValue").disabled = true;
			}
			jQuery('#unit').val(unit);
			jQuery('#ScadaBasePointmemo').val(ScadaBasePointmemo);
			jQuery('#ScadaBasePointcreateTime').val(ScadaBasePointcreateTime);
			jQuery('#ScadaBasePointModal').modal('show');
		}
	});
	jQuery('.saveScadaBasePointBtn').on('click', function () {
		var scadaRtuBaseInfoId = jQuery('#ScadaRtuBaseInfoId').val();
		var id = jQuery('#ScadaBasePointId').val();
		var pointType = jQuery('#pointType').val();
		var name = jQuery('#ScadaBasePointname').val();
		var alias = jQuery('#ScadaBasePointalias').val();
		var dataField = jQuery('#dataField').val();
		var DataType = jQuery('#DataType').val();
		var SwitchingValue = jQuery('#SwitchingValue').val();
		var UpperLimit = jQuery('#UpperLimit').val();
		var lowLimit = jQuery('#lowLimit').val();
		var unit = jQuery('#unit').val();
		var memo = jQuery('#ScadaBasePointmemo').val();
		var createTime = jQuery('#ScadaBasePointcreateTime').val();
		var d;
		if(id == ""){
			d = {
				"scadaRtuBaseInfoId" : scadaRtuBaseInfoId,
				"pointType" : pointType,
				"name" : name,
				"alias" : alias,
				"dataField" : dataField,
				"dataType" : DataType,
				"switchingValue" : SwitchingValue,
				"upperLimit" : UpperLimit,
				"lowLimit" : lowLimit,
				"unit" : unit,
				"memo" : memo,
				"createTime" : new Date(),
			};
		}else{
			d = {
				"id" : id,	
				"scadaRtuBaseInfoId" : scadaRtuBaseInfoId,
				"pointType" : pointType,
				"name" : name,
				"alias" : alias,
				"dataField" : dataField,
				"dataType" : DataType,
				"switchingValue" : SwitchingValue,
				"upperLimit" : UpperLimit,
				"lowLimit" : lowLimit,
				"unit" : unit,
				"memo" : memo,
				"createTime" : createTime,
			};
		}
		var url=window.rootPath+'/ui/scadartubase/saveScadaBasePoint';
        jQuery.ajax({
            url:encodeURI(url),
            type:'post',
            data:JSON.stringify(d),
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success: function () {
                jQuery('#ScadaBasePointModal').modal('hide');
                jQuery('#ScadaBasePointId').val('');
                jQuery('#ScadaBasePointcreateTime').val('');
                jQuery('#pointType').val('');
        		jQuery('#ScadaBasePointname').val('');
        		jQuery('#ScadaBasePointalias').val('');
        		jQuery('#scadareal').val("pointOne");
        		jQuery('#DataType').val("1");
        		jQuery('#SwitchingValue').val("无");
        		jQuery('#UpperLimit').val('');
				jQuery('#lowLimit').val('');
				document.getElementById("UpperLimit").disabled = true;
				document.getElementById("lowLimit").disabled = true;
				document.getElementById("SwitchingValue").disabled = false;
        		jQuery('#unit').val('');
        		jQuery('#ScadaBasePointmemo').val('');
        		serverScadaBasePointTable.draw();
            }
        });
	});
	//删除点位
	jQuery('#delScadaBasePointBtn').on('click', function () {
		var selects = getSelect("#ScadaBasePointTable", 1);
		if(selects.length==0){
			alert("请选择删除的节点！");
		}else if(selects.length>0){
			var ids = "";
			for (var i = 0; i < selects.length; i++) {
				ids += selects[i].id + ",";
			}
			ids = ids.substring(0, ids.length-1);
			$.springLemonDialog({
		         content: '确认删除'+selects.length+'条点位？',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		        	 if (index == 0) {
		        		 var url=window.rootPath+"/ui/scadartubase/ScadaBasePoint"+"?ids="+ ids;
	                        jQuery.ajax({
	                            url:encodeURI(url),
	                            cache:false,
	                            type : "get",
	                            dataType:"json",
	                            success:function(data){
	                            	serverScadaBasePointTable.draw();
	                            }
	                        });
		        	 }
		             $(this).closeDialog(modal);
		         }
		       }); 
		}
	});
	//删除单元
	jQuery('.delscadaBtn').on('click', function () {
		var selects = getSelect("#dataTable", 1);
		if(selects.length==0){
			alert("请选择删除的数据采集单元！");
		}else if(selects.length>0){
			var ids = "";
			for (var i = 0; i < selects.length; i++) {
				ids += selects[i].id + ",";
			}
			ids = ids.substring(0, ids.length-1);
			$.springLemonDialog({
				content: '确认删除'+selects.length+'条数据采集单元？',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					if (index == 0) {
						var url=window.rootPath+"/ui/scadartubase/ScadaRtuBaseInfo"+"?ids="+ ids;
						jQuery.ajax({
							url:encodeURI(url),
							cache:false,
							type : "get",
							dataType:"json",
							success:function(data){
								ScadaRtuBaseInfoId = "";
								jQuery('#ScadaRtuBaseInfoId').val('');
								serverScadaBasePointTable.draw();
								serverTable.draw();
							}
						});
					}
					$(this).closeDialog(modal);
				}
			}); 
		}
	});
});